﻿using UnityEngine;
using System.Collections.Generic;

/*
 * GlobalID
 * 提供绝不重复的全局ID
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/13/2017 3:11:19 PM
 */
namespace TTGameEngine {

public class GlobalID {
    static CountID_ULong m_globalID = new CountID_ULong();

    static public ulong GetGID(ulong needNum = 1) {
        return m_globalID.GetGID(needNum);
    }
}


//GlobalID的非static版。
public class CountID_ULong {
    public static readonly uint UNINIT_VALUE = 0;
    //下次可用的ID。id从1开始，id=0当做是未赋值的初始值。
    ulong id = 1;

    /// <summary>
    /// 如果参数大于1，下次ID会跳过指定个数。
    /// </summary>
    /// <param name="needNum">需要的连续ID个数。</param>
    /// <returns></returns>
    public ulong GetGID(ulong needNum = 1) {
        ulong retGID = id;
        //计算下一次的ID
        if(ulong.MaxValue - id < needNum) {
            id = 1;
        }
        id += needNum;
        return retGID;
    }
}

public class CountID_UInt {
    public static readonly uint UNINIT_VALUE = 0;
    //下次可用的ID。id从1开始，id=0当做是未赋值的初始值。
    uint id = 1;

    /// <summary>
    /// 如果参数大于1，下次ID会跳过指定个数。
    /// </summary>
    /// <param name="needNum">需要的连续ID个数。</param>
    /// <returns></returns>
    public uint GetGID(uint needNum = 1) {
        uint retGID = id;
        //计算下一次的ID
        if(uint.MaxValue - id < needNum) {
            id = 1;
        }
        id += needNum;
        return retGID;
    }
}
}
